package com.lhj.xxsc.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lhj.xxsc.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

	//在支付宝创建的应用的id
	private String app_id = "2016102100733478";

	// 商户私钥，您的PKCS8格式RSA2私钥
	private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCZvXutoJ1aw+99Pqk1ufI+wyxUBIKF/0t9BhxhWQZSjQQZ4925z5KJIVknoifg0D93ZSpjyV7FcqvYXDOZrsKmg/E8PVsf/doHaB0xT+9hsEPzRAZmnNkjYe8RZaDEeJpHHJz75AXEePtl4FQNDavIiZGZEnx9fwAMx38nN/UPHnUcvVFyZPumtWNXRUXYLfcozu5B4W39YRGSUGjHBTvzhaD5s7cShu9fz98UNV7A+xM5/VxSevlktrGpAGtLZsZjQ3aB8QwsVzQi0zjL3a6vHiP64q9v9hOnL35zyQh4VvmEICj4bMbrH+Gdh90GkBuNKcg/2DF24wq9aCUZimyjAgMBAAECggEAFcwz2O5I4TGepxdXTnLK0fNCObdZ+j20KknPhE3nA5VS24jfSxUPJbpAtcnb9ZgRJ5CEbhmDAmO70bVg641Hs+xJK81XOjX3z80HNpKq0iL3v2oJrkrWzpl4F/Nc0/51gVehOmWeG7k7i7o+sIsbrBc1OYrRc8mp7CXs9VaNvFLQ0KWuDvlpH3cYydNVCtXlkOZ9eePEdWFRiESCJt0vIEi7y6LbYyyvmWfLnz0DfwSXacJcqe8OoAqQ4/IpK7SwGvBLpKTUn/KGIfoC4SMmKuDzk8Qne/BXHKHl7YpusE2Yg7q3BfSEP/tt7LDxQMOw933GhaKOGSU+rzBPMAHHwQKBgQDTicLTeGO83vFvzr2lR8VoL+9sUv/iuzTEDVgDlS82cSSx4VTy9CVVd4mKLOlX/3YXRiimk/jgXTW4RHWSy1Nz1QpBEHBFU51EjuFZCTc5E9ZaG4ezuUoMxbsISgbS65U+g4M3PbE40n0n7CWiOEd57dEmVpEU5FB3istsOCafSwKBgQC6DckMAmUV+zlPTnMpJ77V+cDOSvnVgNRH9zGiQ5jN7UddZcTzVum8H2R/sEnbcg+pdNQzzrczLA7jnRowK3/ES24I5VAISQvCRV3ZSCjZEDq3jyKwxf60bCv89L3Ldbd1idYQPKyBvWWe6REtbJ5SMGg5yUoYLDZzVlhxBRGZCQKBgQC3QaYFW0tUSPNyP/EtKUMMIlLRTgodmr54hXLVD72p9VSrwSC9pOJe60K88HFp6YadXQtEW/aEMFukebI9bGWseEf828fXVztsNDdm0cQEb3oH78ec3A0ZiYahBLhqqupNENNw3MIWDsub9UkZApZxO6QwCViAd9Z56USXSQG0hwKBgAl5NDh5Ur5YyWVBYFnDHVN14Rxf8vKtGaIEczGI8ZnisBSQXwPo9APRMWPTDR+wYh3FxhxRNUDUyHlMgY3cWSgckyC+1GFnnsKol4u9wd6zldnM3v5TRpHXRDIgIB3hA5Nnjk/LVxlQFIGP6oD+m90he90dY7lxXuBmkCBJGslJAoGBALR3tLUQdZxKYYdLzh346NbnY9KJiTkIg/ESp4aEcoag8Jese/KFNBJHG4nfWq21pgEkRabbKaH/G/1UpSGEkmr/59H6QJEkmUkVqKWKrZDEL0L/mW/7VkZdMVe3dFPgljIQgbXES4HfO4RzIpAhD1MzmmguQK2KDgrl/je9BDYG";
	// 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
	private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjlWVAZGNkRHiZ7kmLl8W0/XgDDaG2vA6z0Q+niCfLPI7AYpbQuovXpg1tGN3pQDVFepWJszlFgUKZxXrWxXKonCjtzZX6Rze4P+rom1BgnbXfGgu/2a3wbw1UWimYJ5a13KdA6Px3D0YE7pvvDG0/5VSc42HDQhhIO4XeNPxZyBT+IZl++uPO9ME0nQ9IY4o/0onay/KLL/Fh4dEenCfWi5Dyp3NFu9AKxBmkU0NPebDIKL0bHPanl/VxXT2yQziommCQBeRbz7zZqPfIvl5K0vDx6F+6mH32T8kvQEdu9XI5AB1lSkSjj3HMGEXA/TV9k5t1Kq6F5H4Lc0pndzQcQIDAQAB";
	// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	// 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
	private String notify_url;

	// 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	//同步通知，支付成功，一般跳转到成功页
	private String return_url;

	// 签名方式
	private String sign_type = "RSA2";

	// 字符编码格式
	private String charset = "utf-8";

	// 收单时间
	private String timeout = "30m";

	// 支付宝网关； https://openapi.alipaydev.com/gateway.do
	private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

	public String pay(PayVo vo) throws AlipayApiException {

		//AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
		//1、根据支付宝的配置生成一个支付客户端
		AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
				app_id, merchant_private_key, "json",
				charset, alipay_public_key, sign_type);
		//2、创建一个支付请求 //设置请求参数
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(return_url);
		alipayRequest.setNotifyUrl(notify_url);
		//商户订单号，商户网站订单系统中唯一订单号，必填
		String out_trade_no = vo.getOut_trade_no();
		//付款金额，必填
		String total_amount = vo.getTotal_amount();
		//订单名称，必填
		String subject = vo.getSubject();
		//商品描述，可空
		String body = vo.getBody();
		alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
				+ "\"total_amount\":\"" + total_amount + "\","
				+ "\"subject\":\"" + subject + "\","
				+ "\"body\":\"" + body + "\","
				+ "\"timeout_express\":\"" + timeout + "\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		String result = alipayClient.pageExecute(alipayRequest).getBody();
		//会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
		System.out.println("支付宝的响应：" + result);

		return result;

	}
}
